package com.example.myretrofit2.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.myretrofit2.Goods;
import com.example.myretrofit2.MainActivity;
import com.example.myretrofit2.R;
import com.example.myretrofit2.adapter.ViewpagerAdapter;
import com.example.myretrofit2.common.MyApplication;
import com.example.myretrofit2.fragment.CarFragment;
import com.example.myretrofit2.fragment.GoodsFragment;
import com.example.myretrofit2.presenter.GoodsPresenter;
import com.example.myretrofit2.presenter.MySubscriber;



public class GoodsInfoActivity extends AppCompatActivity {
ViewPager goods_vp;
LinearLayout addCar;
DrawerLayout goodsDraw;
FragmentManager fm;
Bundle bundle;
LinearLayout car;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_info);
        goods_vp = findViewById(R.id.goods_vp);
        car = findViewById(R.id.tab_item_car);
        Intent intent = getIntent();
        int goodsid = intent.getIntExtra("goodsId",0);
        fm = getSupportFragmentManager();
        GoodsPresenter.getGoodsInfo(goodsid, new MySubscriber<Goods>(this) {
            @Override
            public void onNext(Goods goods) {
                if(goods!=null){
                    initDraw(goods);
                    ViewpagerAdapter viewpagerAdapter = new ViewpagerAdapter(goods.getGoodsinfos(), MyApplication.getContext());
                    goods_vp.setAdapter(viewpagerAdapter);
                }

            }
        });
        car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(GoodsInfoActivity.this, MainActivity.class);
                intent.putExtra("car","car");
                startActivity(intent);
//                CarFragment carFragment = new CarFragment();
//
////                FragmentTransaction ft = fm.beginTransaction();
////                ft.replace(R.id.content,carFragment);
////                ft.commit();
            }
        });
    }

    public void initDraw(final Goods goods){
        addCar = findViewById(R.id.goods_item_addCar);
        goodsDraw = findViewById(R.id.goods_draw);
        FragmentTransaction ft = fm.beginTransaction();
        GoodsFragment goodsFragment = new GoodsFragment();
        bundle = new Bundle();
        bundle.putParcelable("goods",goods);
        goodsFragment.setArguments(bundle);
        ft.replace(R.id.goodsFram,goodsFragment);
        ft.commit();
        addCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goodsDraw.openDrawer(Gravity.RIGHT);
            }
        });
    }
    public void closeDrawr(){

    }
    public  static Context getContext(){
        Context context =getContext();
        return context;
    }
}